package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.Grade;
import com.xmy.cultivate.entity.Organization;
import com.xmy.cultivate.entity.excelEntity.WillingnessSurveyExcel;
import com.xmy.cultivate.service.IGradeService;
import com.xmy.cultivate.service.IOrganizationService;
import com.xmy.cultivate.service.IWillingnessSurveyService;
import com.xmy.cultivate.util.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;


@RestController
@RequestMapping("/api/willingnessSurvey")
public class WillingnessSurveyController extends BaseController {

    @Autowired
    IOrganizationService iOrganizationService;

    @Autowired
    IGradeService iGradeService;

    @Autowired
    IWillingnessSurveyService iWillingnessSurveyService;

    String[] quarterArray = {"","寒假班","春季班","暑假班","秋季班"};

    String[] subjectsArray = {"","英语","数学","物理"};

    @GetMapping()
    @ApiOperation("续班意愿度")
    public Result index(@RequestParam Map<String,Object> reMap) {
        String schoolId = reMap.get("schoolId") != null && !reMap.get("schoolId").toString().isEmpty() ? reMap.get("schoolId").toString() : null;
        if (StringUtils.isBlank(schoolId)) {
            reMap.put("school_id",this.baseSchoolIdList);
        }
        PageResult<WillingnessSurveyExcel> willingnessSurvey = iWillingnessSurveyService.getWillingnessSurvey(reMap);
        if (willingnessSurvey.getRows() != null && !willingnessSurvey.getRows().isEmpty()) {
            PageResult<WillingnessSurveyExcel> data = getData(willingnessSurvey);
            return new Result(Code.OK, data);
        }
        return new Result(Code.OK,null,"暂时没有内容");
    }

    @GetMapping("getExcelList")
    public Result getExcelList(@RequestParam Map<String, Object> reMap,
                               HttpServletResponse response) {
        String schoolId = reMap.get("schoolId") != null && !reMap.get("schoolId").toString().isEmpty() ? reMap.get("schoolId").toString() : null;
        if (StringUtils.isBlank(schoolId)) {
            reMap.put("school_id",this.baseSchoolIdList);
        }

        PageResult<WillingnessSurveyExcel> willingnessSurvey = iWillingnessSurveyService.getWillingnessSurvey(reMap);
        if (willingnessSurvey.getRows() != null && !willingnessSurvey.getRows().isEmpty()) {
            PageResult<WillingnessSurveyExcel> data = getData(willingnessSurvey);
            ExcelExportHandleUtil.export(response, "续班意愿度", data.getRows(), WillingnessSurveyExcel.class);
            return new Result(Code.OK, null, "导出成功");
        }
        return new Result(Code.OK, null, "暂无内容");
    }

    private PageResult<WillingnessSurveyExcel> getData(PageResult<WillingnessSurveyExcel> willingnessSurvey) {
        for (WillingnessSurveyExcel willingnessSurveyExcel : willingnessSurvey.getRows()) {
            if (willingnessSurveyExcel.getYearClassId() != null) {
                willingnessSurveyExcel.setYearClassSrc(ConfigData.getYearClassForNum(willingnessSurveyExcel.getYearClassId()));
            }
            if (willingnessSurveyExcel.getQuarterNum() != null) {
                willingnessSurveyExcel.setQuarterStr(quarterArray[willingnessSurveyExcel.getQuarterNum()]);
            }
            if (willingnessSurveyExcel.getSubjectsId() != null) {
                willingnessSurveyExcel.setSubjectsSrc(subjectsArray[willingnessSurveyExcel.getSubjectsId()]);
            }
            Organization organizationServiceById = iOrganizationService.getById(willingnessSurveyExcel.getSchoolId());
            if (organizationServiceById != null) {
                willingnessSurveyExcel.setSchoolSrc(organizationServiceById.getName());
            }
            Grade gradeServiceById = iGradeService.getById(willingnessSurveyExcel.getGradeId());
            if (gradeServiceById != null) {
                willingnessSurveyExcel.setGradeName(gradeServiceById.getName());
            }
        }
        return willingnessSurvey;
    }

}
